//json
var JSON;JSON||(JSON={});
(function(){function k(a){return 10>a?"0"+a:a}function o(a){p.lastIndex=0;return p.test(a)?'"'+a.replace(p,function(a){var c=r[a];return"string"===typeof c?c:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+a+'"'}function m(a,j){var c,d,h,n,g=e,f,b=j[a];b&&"object"===typeof b&&"function"===typeof b.toJSON&&(b=b.toJSON(a));"function"===typeof i&&(b=i.call(j,a,b));switch(typeof b){case "string":return o(b);case "number":return isFinite(b)?""+b:"null";case "boolean":case "null":return""+b;
case "object":if(!b)return"null";e+=l;f=[];if("[object Array]"===Object.prototype.toString.apply(b)){n=b.length;for(c=0;c<n;c+=1)f[c]=m(c,b)||"null";h=0===f.length?"[]":e?"[\n"+e+f.join(",\n"+e)+"\n"+g+"]":"["+f.join(",")+"]";e=g;return h}if(i&&"object"===typeof i){n=i.length;for(c=0;c<n;c+=1)"string"===typeof i[c]&&(d=i[c],(h=m(d,b))&&f.push(o(d)+(e?": ":":")+h))}else for(d in b)Object.prototype.hasOwnProperty.call(b,d)&&(h=m(d,b))&&f.push(o(d)+(e?": ":":")+h);h=0===f.length?"{}":e?"{\n"+e+f.join(",\n"+
e)+"\n"+g+"}":"{"+f.join(",")+"}";e=g;return h}}"function"!==typeof Date.prototype.toJSON&&(Date.prototype.toJSON=function(){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+k(this.getUTCMonth()+1)+"-"+k(this.getUTCDate())+"T"+k(this.getUTCHours())+":"+k(this.getUTCMinutes())+":"+k(this.getUTCSeconds())+"Z":null},String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(){return this.valueOf()});var q=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
p=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,e,l,r={"\u0008":"\\b","\t":"\\t","\n":"\\n","\u000c":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},i;"function"!==typeof JSON.stringify&&(JSON.stringify=function(a,j,c){var d;l=e="";if("number"===typeof c)for(d=0;d<c;d+=1)l+=" ";else"string"===typeof c&&(l=c);if((i=j)&&"function"!==typeof j&&("object"!==typeof j||"number"!==typeof j.length))throw Error("JSON.stringify");return m("",
{"":a})});"function"!==typeof JSON.parse&&(JSON.parse=function(a,e){function c(a,d){var g,f,b=a[d];if(b&&"object"===typeof b)for(g in b)Object.prototype.hasOwnProperty.call(b,g)&&(f=c(b,g),void 0!==f?b[g]=f:delete b[g]);return e.call(a,d,b)}var d,a=""+a;q.lastIndex=0;q.test(a)&&(a=a.replace(q,function(a){return"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)}));if(/^[\],:{}\s]*$/.test(a.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
"]").replace(/(?:^|:|,)(?:\s*\[)+/g,"")))return d=eval("("+a+")"),"function"===typeof e?c({"":d},""):d;throw new SyntaxError("JSON.parse");})})();

//cookie
function cookie(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = cookies[i];
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

function extend(a, b){
    for (var p in b) {
        a[p] = b[p];
    }
    
    return a;
}

(function(Cfg){
if (!Cfg) return;

var win = this;

function getCfg(key) {
    return Cfg[key];
}
    
var reporter = (function() {
    //上报地址
    var url = 'http://beacon.ziyou.com/a.gif',

    //图片对象
        img,

        //用户ID，如果未登录，生成一个随机ID
        uid,

        //初始化上报参数
        reqOpt = {
            //pjt: 'ziyou',
            xt: 'stay'
        };

    //生成随机ID
    function genId(num) {
        var str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
        var len = str.length;

        var num = num || 16;

        var chars = ['u'], at;
        for (var i = 0 ;i< num ;i++)
        {
            at = Math.ceil(Math.random()*len)-1;
            chars.push(str.charAt(at));
        }

        return chars.join('');
    }

    //取得用户ID
    function getUniqueUid() {
         var        
            cookieName = 'report_cid',

            uid = cookie(cookieName);

            if (!uid)
            {
                uid = genId();
                cookie(cookieName, uid, {
                    expires: 90000
                });
            }

        return uid; 
    }

    function report(params) {
        if (!reqOpt.ver) {
            reqOpt.ver = '2.0';
        }
        
        params.random = Math.random();
        params.cid = getUniqueUid();
        params.url = location.href;
        var sinaid = getCfg('uid');
        
        if (sinaid) {
            params.uid = sinaid;
        }

        var opt = extend(reqOpt,params);
        
        for (var i in opt) {
            opt[i] = encodeURIComponent(opt[i]);
        }
        
        img.src = url + '?data=' + encodeURIComponent(JSON.stringify(opt));
    }

    //在线时长汇报
    var olReporter = (function() {
        var timer,
            //上报间隔
            interval = 1800000,

            //初始化状态
            inited = false,
                
            bootTime = new Date().getTime(),

            lastTime = 0,
                
            uid;

        function init() {
            if (inited)
            {
                return;
            }

            uid = getCfg('uid');

            img = new Image();

            inited = true;
        }

        function doReport(opt) {
            var now = new Date().getTime(),

            params = {
                time: now - (lastTime ? lastTime: bootTime)
            };

/*             if (!lastTime && document.referrer)
            {
                params.t_id = document.referrer;
            } */
            if(opt) extend(params, opt);

            report(params);

            lastTime = now;
        }

        return {
            start: function() {
                init();
                
                //首次上报
                doReport({time:0, referer : document.referrer || ''});
                
                timer = win.setInterval(doReport, interval);
            },

            report: function(opt) {
                !inited && init();
                doReport(opt);
            }
        }
    })();

    return {
        start: function() {
            olReporter.start();
        },

        report: function(opt) {
            olReporter.report(opt);
        }
    }
    
})();

reporter.start();
})(window.Cfg);